<?php 
date_default_timezone_set('PRC');
// 数据库操作
$conn = @mysqli_connect('localhost','wd1900144','pass123456');
if($conn==false){
	echo json_encode(['error'=>1,'msg'=>'网站出小差了,请稍后再试！']);
	exit();
}
mysqli_select_db($conn,'wd1900144');
mysqli_query($conn,'set names utf8');

if($_POST['act']=='pub'){
	// 发布动态
	$info = $_POST['info'];
	$pos = $_POST['pos'];
	$tag = rtrim($_POST['tag'],',');
	$time = time();		// 当前时间戳
	$uid = $_POST['uid'];
	// 添加之前处理图片上传
	if(!file_exists('../uploads/')){
		// 创建目录
		mkdir('../uploads/',0777);
	}

	// 保存图片
	$parr = array();
	for($i=1; $i<=3; $i++){
		if(isset($_POST['pic'.$i])){		// isset(变量$_POST['pic2']) 函数，判断变量是否存在
			// 上传图片
			$pic = $_POST['pic'.$i];
			if(strstr($pic,',')){
				$arr = explode(',', $pic);  // 字符串转数组
				$pic = $arr[1];		//判断是否有逗号 如果有就截取后半部分
			}
			$name = date('YmdHis').mt_rand(10000,99999).'.png';   // mt_rand()  随机数
			// 创建文件（如：1.png）,在往文件里面写入内容     file_put_contents('文件地址+名','内容')
			file_put_contents('../uploads/'.$name, base64_decode($pic));  
			// base64_decode(base64文本)
			$parr[] = $name;  // 把3张图片名称，追加到数组里面
		}
	}
	$pics = implode(',', $parr);	//把数组分割成字符串，逗号隔开
	mysqli_query($conn,"insert into so_dynamic(descs,pos,tag,addtime,pic,uid) values('$info','$pos','$tag',$time,'$pics','$uid')");
	echo json_encode(['error'=>0,'msg'=>'发布成功']);
}else if($_POST['act']=='list'){
	// 是请求数据
	$pn = $_POST['pn'];
	$size = $_POST['size'];	// 每页查询多少条数据
	$uid = $_POST['uid']>0?$_POST['uid']:0;//获取用户ID如果没有登录id为零

	$start = ($pn-1)*$size;
	$query = mysqli_query($conn,"select * from so_dynamic as d left join so_user as u on d.uid=u.uid where 1 order by addtime desc limit $start,$size");	
	$list = array();
	while($data=mysqli_fetch_assoc($query)){
		//$data 是每条数据
		$data['addtime'] = format_date($data['addtime']);
		$data['tag'] = explode(',', $data['tag']);	// 转成数组
		$data['pic'] = explode(',', $data['pic']);

		//循环这里，查询点赞表，是否有状态
		$did = $data['did'];//?
		$query2 = mysqli_query($conn,"select lid from so_likes where uid=$uid and did=$did");
		$zan = 0;
		if(mysqli_num_rows($query2)>0){
			$zan=1;
		}
		$data['zan']=$zan;
		//登陆者是否对动态发布者有关注
		$touid= $data['uid'];
		$query3 =  mysqli_query($conn,"select * from so_focus where uid=$uid and touid=$touid");
		$gz = 0;
		if(mysqli_num_rows($query3)>0){
			//查询有结果则视为已关注
			$gz = 1;
		}
		$data['gz'] =$gz;
		// var_dump($query3);
		//$data['gz']是原本数据库没有的，额外添加，用来返回给前端
		$list[] = $data;	// $list 是三维数组
	}

	echo json_encode(['error'=>0,'msg'=>'success','data'=>$list]);
}
else if($_POST['act']=='zan'){
	$uid = $_POST['uid'];
	$did = $_POST['did'];//POST一点要大写!!!
	$query = mysqli_query($conn,"select * from so_likes where uid=$uid and did=$did");

	if(mysqli_num_rows($query)>0){
		//判断是否已经点赞，是的话就取消赞
		mysqli_query($conn,"delete from so_likes where uid=$uid and did=$did");
	}else{
		mysqli_query($conn,"insert into so_likes(uid,did) values($uid,$did)");

	}
	//统计点赞表，关于当前动态点赞总数
	$query2 = mysqli_query($conn,"select count(lid) as c from so_likes where did=$did");
	$cinfo = mysqli_fetch_assoc($query2);	// 一维数组
	
	$num = $cinfo['c'];
	// var_dump($num);
	mysqli_query($conn,"update so_dynamic set zannum=$num where did=$did");
	// var_dump($num);
	echo json_encode(['error'=>0,'msg'=>'success','data'=>$num]);


}
else if($_POST['act']=='focus'){
	$uid = $_POST['uid'];
	$touid = $_POST['touid'];//POST一点要大写!!!
	$query = mysqli_query($conn,"select * from so_focus where uid=$uid and touid=$touid");

	if(mysqli_num_rows($query)>0){
		//判断是否已经关注，是的话就取消赞
		mysqli_query($conn,"delete from so_focus where uid=$uid and touid=$touid");
	}else{
		mysqli_query($conn,"insert into so_focus(uid,touid) values($uid,$touid)");

	}
	$query3 = mysqli_query($conn,"select count(fid) as c from so_likes where touid=$touid");
	$cinfo = mysqli_fetch_assoc($query2);	// 一维数组
	$num = $cinfo['c'];


	echo json_encode(['error'=>0,'msg'=>'success','data'=>$num]);


}
else if($_POST['act']=='we'){


	$query = mysqli_query($conn,"select lid from so_dynamic where did=$did");
	$num = mysqli_num_rows($query);
	$list = array();
	$data['num'] =$num;
	$list[] = $data;
	echo json_encode(['error'=>0,'msg'=>'success','data'=>$list]);
}


mysqli_close($conn);


function format_date($time){
    $t=time()-$time;
    $f=array(
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.'前';
        }
    }
}
 ?>